package com.strutor.linked;

public class BaseLinked<E> {
    private class Node {
        public E e;
        public Node next;

        public Node(E e, Node next) {
            this.e = e;
            this.next = next;
        }

        public Node(E e) {
            this(e, null);
        }

        public Node() {
            this(null, null);
        }

        @Override
        public String toString() {
            return e.toString();
        }
    }

    private Node dummyHead;

    private int size;

    public BaseLinked() {
        this.dummyHead = new Node(null, null);
        this.size = 0;
    }

    public int getSize() {
        return size;
    }

    public boolean isEmpty() {
        return dummyHead.next == null;
    }


    public void add(int index, E e) {
        if (index < 0 || index > size) {
            throw new IllegalArgumentException("添加失败，传入的index不合法");
        }
        Node prev = dummyHead;
        for (int i = 0; i < index; i++) {
            prev = prev.next;
        }
        prev.next = new Node(e, prev.next);
        size++;
    }

    public void addFirst(E e) {
        add(0, e);
    }

    public void addLast(E e) {
        add(size, e);
    }

    public E removeElememt(int index) {
        if (index < 0 || index >= size) {
            throw new IllegalArgumentException("移除失败，传入非法参数");
        }
        Node prev = dummyHead;
        for (int i = 0; i < index; i++) {
            prev = prev.next;
        }
        Node delEmement = prev.next;
        prev.next = delEmement.next;
        delEmement.next = null;
        size--;
        return delEmement.e;
    }

    public E removeFirst() {
        return removeElememt(0);
    }

    public E removeLast() {
        return removeElememt(size - 1);
    }

    public E get(int index) {
        if (index < 0 || index >= size) {
            throw new IllegalArgumentException("获取异常，传入非法参数");
        }
        Node cur = dummyHead.next;
        for (int i = 0; i < index; i++) {
            cur = cur.next;
        }
        return cur.e;
    }


    public E getFirst() {
        return get(0);
    }

    public E getLast() {
        return get(size);
    }

    public void set(int index, E e) {
        if (index < 0 || index >= size) {
            throw new IllegalArgumentException("设置数据异常，传入非法参数");
        }
        Node cur = dummyHead.next;
        for (int i = 0; i < index; i++) {
            cur = cur.next;
        }
        cur.e = e;
    }

    public boolean contains(E e) {
        Node cur = dummyHead.next;
        while (cur != null) {
            if (cur.e == e) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }


    @Override
    public String toString() {
        StringBuilder res = new StringBuilder("LinkedList [");
        Node cur = dummyHead.next;
        while (cur != null) {
            res.append(cur + "->");
            cur = cur.next;
        }
        res.append("NULL");
        res.append("]");
        return res.toString();
    }

    public static void main(String[] args) {
        BaseLinked<Integer> integerBaseLinked = new BaseLinked<>();
        for (int i = 0; i < 10; i++) {
            integerBaseLinked.addLast(i);
            System.out.println(integerBaseLinked.toString());
        }
        integerBaseLinked.addFirst(100);
        System.out.println(integerBaseLinked.toString());
        for (int i = 0; i < 5; i++) {
            integerBaseLinked.removeElememt(2);
            System.out.println(integerBaseLinked.toString());
        }
    }
}
